package team8.fitdroid;

import java.util.LinkedList;

import team8.database.databaseconnector;
import team8.helpers.timehelper;
import team8.helpers.user;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class configuration extends Activity{
	
	private Button savebutton;
	private Button backbutton;
	private EditText nameedit;
	private EditText ageedit;
	private EditText weightedit;
	private EditText speededit;
	private EditText distanceedit;
	private EditText timeedit;
	private databaseconnector database;
	private user runner ;
	private timehelper timer = new timehelper();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuration);
        
        database= new databaseconnector(this);
        runner= new user(database);
        
        savebutton= (Button)findViewById(R.id.savebutton);
        savebutton.setOnClickListener(savebuttonListener);
        
        backbutton= (Button)findViewById(R.id.backbutton);
        backbutton.setOnClickListener(backbuttonListener);
        
        nameedit = (EditText)findViewById(R.id.nameedit);
        ageedit = (EditText)findViewById(R.id.ageedit);
        weightedit = (EditText)findViewById(R.id.weightedit);
        speededit = (EditText)findViewById(R.id.speededit);
        speededit.setEnabled(false);
        
        distanceedit = (EditText)findViewById(R.id.distanceedit);
        distanceedit.setEnabled(false);
        
        timeedit = (EditText)findViewById(R.id.timeedit);
        timeedit.setEnabled(false);        
        showdata();
	}
	
	 private OnClickListener savebuttonListener = new OnClickListener() {
	        public void onClick(View v) {
	          // do something when the button is clicked
	        	Intent i = new Intent(configuration.this, main.class);
	        	savedata();
	            startActivity(i);
	        }
	    };
	    
	    private OnClickListener backbuttonListener = new OnClickListener() {
	        public void onClick(View v) {
	          // do something when the button is clicked
	        	Intent i = new Intent(configuration.this, main.class);
	            startActivity(i);
	        }
	    };
	 
	private void showdata()
	{
		LinkedList<String> tmp = runner.getuserdata();
		if (tmp.size()>0)
		{
			nameedit.setText(tmp.get(0));
			ageedit.setText(tmp.get(3));
			weightedit.setText(tmp.get(2));
			
			distanceedit.setText(tmp.get(1));
			timeedit.setText(tmp.get(4));
			
			speededit.setText(tmp.get(5));
		}
	}
	
	private void savedata()
	{
		runner.storeuserdata(nameedit.getText().toString(), 0, Integer.valueOf(weightedit.getText().toString().trim()), Integer.valueOf(ageedit.getText().toString().trim()), "00:00");
	}
	@Override
    public void onDestroy()
	{
		database.close();
		super.onDestroy();
	}
}
